package com.study.d012Components;

import java.util.ArrayList;
import java.util.List;

import com.entity.Student;

import javafx.application.Application;
import javafx.beans.Observable;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

/**
*@description 微调控件    48课
*@author xtayhame
*date 2020年3月3日
*version
*since v1.0
*/
public class SpinnerMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage stage) throws Exception {
		Student s1= new Student("A", 10);
		Student s2= new Student("B", 11);
		Student s3= new Student("C", 12);



		AnchorPane ap =new AnchorPane();
		/*
		//设置从0开始，最大值为10,默认值为1
		Spinner spinner =new Spinner<>(0, 10, 1);
		//设置每次的微调数是2
		Spinner<Integer> spinner =new Spinner<Integer>(0, 10, 1, 2);

		*/

		ObservableList<String> names=FXCollections.observableArrayList();
		names.add("张三");
		names.add("李四");
		names.add("王五");

		Spinner<String> spinner = new Spinner<String>(names);
		//设置微调的样式， 貌似win10不起作用
		spinner.setStyle(Spinner.STYLE_CLASS_SPLIT_ARROWS_HORIZONTAL);

		//监听数据的变化
/*		spinner.valueProperty().addListener(new ChangeListener<Integer>() {
			@Override
			public void changed(ObservableValue<? extends Integer> observable, Integer oldValue, Integer newValue) {
				System.out.println("当前选的是："+newValue);
			}
		});
*/
		 ObservableList<Student> list=FXCollections.observableArrayList();
		 list.add(s1);
		 list.add(s2);
		 list.add(s3);

		 Spinner<Student> spinner3 = new Spinner<Student>();

		 MySpinnerValueFactory mySpinnerValueFactory = new MySpinnerValueFactory(list);
		 //使用自己实现的SpinnerValueFactory
		 //spinner.setValueFactory(mySpinnerValueFactory);

		 //也使用SpinnerValueFactory类中自带的内部类，
		 //	    SpinnerValueFactory.DoubleSpinnerValueFactory,
		 //		SpinnerValueFactory.IntegerSpinnerValueFactory,
		 //		SpinnerValueFactory.ListSpinnerValueFactory
		 SpinnerValueFactory.ListSpinnerValueFactory<Student> svlvf= new SpinnerValueFactory.ListSpinnerValueFactory<Student>(list);


		 svlvf.setConverter(new StringConverter<Student>() {

			@Override
			public String toString(Student object) {
				if(object ==null){
					return "";
				}
				return object.getName() + "-" + object.getAge();
			}

			@Override
			public Student fromString(String string) {
				// TODO Auto-generated method stub
				return null;
			}
		});


		 //spinner.setEditable(true);

		 //对Student对象进行转换
		 mySpinnerValueFactory.setConverter(new StringConverter<Student>() {

			@Override
			public String toString(Student object) {
				if(object !=null){
					Student s = (Student)object;
					return s.getName() + "-" + s.getAge();
				}
				return "";
			}

			@Override
			public Student fromString(String str) {
				//当设置spinner.setEditable(true)时， 此时的参数为输入的值
				//可以通过当前值进行录入操作
				System.out.println("输入的名称是:"+str);
				//这里还是要判断一下，要不然会重复录入
				//这里有问题，暂时不处理
				Student newStu=null;
				for(Student stu:list){
					if(stu.getName().equals(str)){
						newStu=stu;
						break;
					}
				}
				if(newStu==null){
					newStu =  new Student(str, 19);
					list.add(newStu);
				}
				return newStu;
			}
		});

		 //设置默认值，注意，这个方法要写在setConverter()之后，否则不能正常显示
		 mySpinnerValueFactory.setValue(s3);
		//spinner.setValueFactory(new MyScheduledService(studetns));

		 spinner3.setValueFactory(svlvf);

		AnchorPane.setTopAnchor(spinner, 10.0);
		AnchorPane.setTopAnchor(spinner3, 40.0);
		ap.getChildren().add(spinner);
		ap.getChildren().add(spinner3);
		stage.setScene(new Scene(ap, 400, 500));
		stage.show();
	}

}

class MySpinnerValueFactory extends SpinnerValueFactory<Student>{
	private  int index=-1;

	List<Student> list = null;

	public MySpinnerValueFactory(List<Student> studetns ) {
		this.list = studetns;
	}


	@Override
	public void decrement(int steps) {
		System.out.println("decrement->" + index);
		index = index - steps;
		if(index<=-1){
			index = 0;
		}
		this.setValue(list.get(index));
	}

	@Override
	public void increment(int steps) {
		System.out.println("increment->" + index);
		index = index + steps;
		if(index >=list.size()){
			index = list.size()-1;
		}
		this.setValue(list.get(index));
	}


}
